#!/bin/bash

notarget ()
{
   echo 'Please specify a target release folder.';
   echo '  ex: CreateRelease 04.00';
   exit 1
}

releaseexists () 
{
   echo 'The Release '$1' already exists.';
   exit 2
}

invalidbuild ()
{
  echo 'Please specify a valid output folder.';
  echo '  ex: CreateRelease 04.00 debug';
  echo '  ex: CreateRelease 04.00 release';
  exit 3
}

if [ ! $1 ]; then
  notarget
fi

build='Debug'

if [ $2 ]; then
  if [ $2 = 'debug' ]; then
    build='Debug'
  elif [ $2 = 'release' ]; then
    build='Release'
  else
    invalidbuild
  fi
fi

release_parent='./releases'

if [ ! -d $release_parent ]; then
   mkdir releases
fi

release_folder=$release_parent/$1

if [ ! -d $release_folder ]; then
   #releaseexists
#else
   mkdir $release_folder
#   mkdir $release_folder/Source
#   mkdir $release_folder/Config
#   mkdir $release_folder/Transforms

   cp OR.Lazarus.Presentation/bin/$build/*.dll $release_folder
   cp OR.Lazarus.Presentation/bin/$build/*.exe $release_folder
#   cp -f -R OR.Lazarus.Presentation/Source $release_folder
#   cp -f -R OR.Lazarus.Presentation/Config $release_folder
#   cp -f -R Transforms $release_folder

   mv $release_folder/OR.Lazarus.Presentation.exe $release_folder/lazarus.exe
fi

echo 'Release '$1' Created'

exit 0
